/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.world;

import cz.fi.muni.diplomka.common.Coordinates;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.dto.Tile;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.AlliancesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.WorldManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.AlliancesManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.WorldManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.EmptyPanel;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.FeedbackPanel;

/**
 *
 * @author Jakub Kutil
 */
public class TilePage extends WebPage {

    private WorldManager worldManager = new WorldManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private AlliancesManager alliancesManager = new AlliancesManagerImpl();

    public TilePage(final PageParameters parameters) {
        add(new FeedbackPanel("feedback"));

        Long id = new Long(0);
        int x = 0;
        int y = 0;

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));
            PageParameters p = new PageParameters();
            p.add("id", parameters.getString("id"));
            add(new LinkPanel("linkPanel", p));


            if (parameters.containsKey("x")) {
                x = Integer.parseInt(parameters.getString("x"));
            }

            if (parameters.containsKey("y")) {
                y = Integer.parseInt(parameters.getString("y"));
            }

            Tile tile = worldManager.getTileAtPosition(x, y);

            add(new Label("nameLabel", tile.getType().toString() + " at [" + tile.getCoordinates().getX_coordinate() + "," + tile.getCoordinates().getY_coordinate() + "]"));

            switch (tile.getType()) {
                case plain:
                    add(new Label("infoLabel", "Here you can build new city."));
                    p = new PageParameters();
                    p.add("id", id.toString());
                    p.add("x", Integer.toString(x));
                    p.add("y", Integer.toString(y));
                    if(parameters.containsKey("cityName")){
                    p.add("cityName", parameters.getString("cityName"));
                    }
                    add(new BuildPanel("buildPanel", p));
                    break;
                case mountain:
                    add(new Label("infoLabel", "Send army here to get some stone."));
                    add(new EmptyPanel("buildPanel"));
                    break;
                case village:
                    add(new Label("infoLabel", "Send army here to get some food."));
                    add(new EmptyPanel("buildPanel"));
                    break;
                case fortress:
                    add(new Label("infoLabel", "Send army here to get some gold."));
                    add(new EmptyPanel("buildPanel"));
                    break;
                case city:
                    Coordinates c = new Coordinates();
                    c.setX_coordinate(x);
                    c.setY_coordinate(y);
                    City city = cityManager.getCityAtPosition(c);
                    String info = "City " + city.getName() + " owned by " + cityManager.getOwnerOfCity(city.getId()).getNickName();
                    if (alliancesManager.getAllianceOfPlayer(id).getId() != null) {
                        info += ", member of " + alliancesManager.getAllianceOfPlayer(id).getName();
                    }
                    add(new Label("infoLabel", info));
                    add(new EmptyPanel("buildPanel"));
                    break;

            }
        }
    }
}
